package com.lmy.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmy.common.utils.PageUtils;
import com.lmy.common.utils.Query;
import com.lmy.common.utils.R;
import com.lmy.ware.entity.WareSkuEntity;
import com.lmy.ware.feign.ProductFeignService;
import com.lmy.ware.mapper.WareSkuMapper;
import com.lmy.ware.service.WareSkuService;
import com.lmy.ware.vo.SkuStockVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuMapper, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuMapper wareSkuMapper;
    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //如果数据库没有数据，插入数据
        List<WareSkuEntity> wareSkuEntities = wareSkuMapper.selectList(new LambdaQueryWrapper<WareSkuEntity>()
                .eq(WareSkuEntity::getSkuId, skuId)
                .eq(WareSkuEntity::getWareId, wareId));
        if (wareSkuEntities.isEmpty()) {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(0);
            //todo 远程查询sku_name
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode()==0){
                    wareSkuEntity.setSkuName(skuInfo.get("skuName").toString());
                }
            } catch (Exception e) {

            }

            wareSkuMapper.insert(wareSkuEntity);
        } else {
            wareSkuMapper.addStock(skuId, wareId, skuNum);
        }
    }

    @Override
    public List<SkuStockVo> getSkuStock(List<Long> skuIds) {
        List<SkuStockVo> stockVoList = skuIds.stream().map(skuId -> {
            SkuStockVo skuStockVo = new SkuStockVo();
            long count = wareSkuMapper.getSkuStock(skuId);
            skuStockVo.setHasStock(count>0);
            skuStockVo.setSkuId(skuId);
            return skuStockVo;

        }).collect(Collectors.toList());
        return stockVoList;
    }

}